window.onload = function(){
	//实现下拉
	var select_info = document.getElementById("select_info");
	var select_obj = document.getElementById("select_obj");
	var select_main = document.getElementById("select_main");
	var is = select_main.children;
	var btn = document.getElementById("btn");
	var span_obj = document.getElementById("span_obj");
	//点击的时候出现
	btn.onclick = function(){
		select_main.style.height = "88px";
		select_main.style.transition = "all 1s";
	}
	//	离开的时候隐藏
	span_obj.onmouseleave = function(){
		select_main.style.height = "0px";
		select_main.style.transition = "all 0.3s";
	}
	
	//  实现option
	for(var i = 0; i < is.length; i++){
		is[i].index = i;
		is[i].onclick = function(){
			select_obj.innerHTML = is[this.index].innerHTML;
		}
	}
	
	
	

	
//	获取本地存储
	var obj_ul = document.getElementById("obj_ul");
	var pro_info = get("pro_info");
	if(pro_info){
		setShouCar(pro_info);
	}
	function setShouCar(pro_info){
		for(var i = 0; i < pro_info.length; i++){
			var li = document.createElement("li");
			obj_ul.appendChild(li);
			for(var j = 0; j < 7; j++){
				var p = document.createElement("p");
				li.appendChild(p);
			}
			var ps = li.getElementsByTagName("p");
			//选择框
			var input = document.createElement("input");
			input.type = "checkbox";
			input.className = "all_obj";
			ps[0].appendChild(input);
			
			//图片
			var a = document.createElement("a");
			ps[1].appendChild(a);
			var img = document.createElement("img");
			img.src = pro_info[i].change_img_src;
			a.appendChild(img);
			
			//文本
			ps[2].innerHTML = pro_info[i].test;
			
			//单价
			var span = document.createElement("span");
			span.className = "dj";
			var arr_s = pro_info[i].price.split("￥");
			span.innerHTML = arr_s[1] + ".00";
			ps[3].appendChild(span);
			
			
			//输入框
			var span_2 = document.createElement("span");
			span_2.innerHTML = "-";
			span_2.className = "reduce";
			ps[4].appendChild(span_2);
			var input_2 = document.createElement("input");
			input_2.value = pro_info[i].pro_num;
			input_2.className = "nums";
			ps[4].appendChild(input_2);
			var span_3 = document.createElement("span");
			span_3.innerHTML = "+";
			span_3.className = "add";
			ps[4].appendChild(span_3);
			
			//小计
			var span_4 = document.createElement("span");
			
			span_4.innerHTML = parseInt(pro_info[i].pro_num) * parseInt(arr_s[1]) + ".00";
			span_4.className = "xj";
			ps[5].appendChild(span_4);
			
			var a_2 = document.createElement("a");
			var span_5 = document.createElement("span");
			a_2.className = "remove_obj";
			a_2.href = "javascript:void(0)";
			a_2.innerHTML = "删除";
			span_5.innerHTML = "移到我的关注";
			ps[6].appendChild(a_2);
			ps[6].appendChild(span_5);
		}
	}
	
	
	
	//	实现全选功能
	var all = document.getElementsByClassName("all");
	var all_obj = document.getElementsByClassName("all_obj");
	var arr = [];
	var arr_2 = [];
	alls(all[0],all[1],all_obj);
	alls(all[1],all[0],all_obj);
	//	点击以后全选的方法
	function alls(obj,obj_sibling,all_obj){
		obj.onclick = function(){
			if(this.checked === true){
				all_chooes(this.checked,all_obj);
				obj_sibling.checked = true;
				sum_price(all_obj, inputs, sum, xj);
			}else if(this.checked === false){
				all_chooes(this.checked,all_obj);
				obj_sibling.checked = false;
				clear_sum();
			}
		}
	}
	
	//	取消全选
	for(var i = 0; i < all_obj.length; i++){
		all_obj[i].onclick = function(){
			if(this.checked === false){
				clear_all(all);
			}
		}
	}
	
	//	取消全选的方法
	function clear_all(obj){
		for(var i = 0; i < obj.length; i++){
			obj[i].checked = false;
		}
	}
//	所有都选中时全选

	for(var i = 0; i < all_obj.length; i++){
		all_obj[i].index = i;
		all_obj[i].onclick = function(){
			//全选
			all_2(all_obj, all);
//			小计
	
			

		}
	}
	function all_2(all_obj, obj){
		arr = [];
		arr_2 = [];
		for(var i = 0; i < all_obj.length; i++){
			if(all_obj[i].checked){
				arr.push(all_obj[i].checked);
				sum_price(all_obj, inputs, sum, xj);
			}else if(!all_obj[i].checked){
				arr_2.push(all_obj[i].checked);
			}
		}
		
		if(arr.length === all_obj.length){
			for(var i = 0; i < obj.length; i++){
				obj[i].checked = true;
			}
		}else {
			for(var i = 0; i < obj.length; i++){
				obj[i].checked = false;
			}
		}
		if(arr_2.length === all_obj.length){
			clear_sum();
		}
	}

	

	
//	全选2
	
	
	
//	全选的方法
	function all_chooes(_this_,obj){
		for(var j = 0; j < obj.length; j++){
			obj[j].checked = _this_;
		}
	}
	

	
	
	
//	输入框
	var all_btn = document.getElementById("all_btn");
	var inputs = all_btn.getElementsByClassName("nums");
	var jia = document.getElementsByClassName("add");
	var jian = document.getElementsByClassName("reduce");
//	限制input不可为非法值
	for(var i = 0; i < inputs.length; i++){
		inputs[i].index = i;
		input_value(inputs[i]);
	}
	
	function input_value(ints){
		ints.onblur = function(){
			if(isNaN(ints.value)){
				ints.value = 1;
			}else if(ints.value > 99){
				ints.value = 99;
			}else if(ints.value < 0){
				ints.value = 1;
			}else if(ints.value == ""){
				ints.value = 1;
			}else if(ints.value == 0){
				ints.value = 1;
			}
			xjs(inputs, this.index);
			sum_price(all_obj, inputs, sum, xj);
		}
	}
	
	for(var i = 0; i < jia.length; i++){
		jia[i].index = i;
		jian[i].index = i;
		jia[i].onclick = function(){
			add(this.index,inputs);
			xjs(inputs, this.index);
			sum_price(all_obj, inputs, sum, xj);
		}
		jian[i].onclick = function(){
			reduce(this.index,inputs);
			xjs(inputs, this.index);
			sum_price(all_obj, inputs, sum, xj);
		}
	}
//	实现累加
	function add(index,ints){
		var v = parseInt(ints[index].value);
		v += 1;
		if(v > 99){
			v = 99;
		}
		ints[index].value = v;
	}
//	实现递减
	function reduce(index,ints){
		var v = parseInt(ints[index].value);
		v -= 1;
		if(v < 1){
			v = 1;
			return false;
		}
		ints[index].value = v;
	}
	
	
	//小计的值
	var xj = document.getElementsByClassName("xj");
	var dj = document.getElementsByClassName("dj");
	function xjs(inpts,index){
		for(var i = 0; i < inpts.length; i++){
			xj[index].innerHTML = parseInt(inputs[index].value) * parseInt(dj[index].innerHTML) + ".00";
		}
	}
	
	
//	总价
	var sum = document.getElementById("sum");
	var s_p = document.getElementById("sum_price");
	var n = 0;
	var s = 0;
//	获取全选时的数量
	function sum_price(all_obj, ints, sum_obj, xj){
		n = 0;
		s = 0;
		for(var i = 0; i < all_obj.length; i++){
			if(all_obj[i].checked === true){
				n += parseInt(ints[i].value);
				s += parseInt(xj[i].innerHTML);
			}
		}
		sum.innerHTML = n;
		s_p.innerHTML = s + ".00";
	}
	
	function clear_sum(){
		sum.innerHTML = "0";
		s_p.innerHTML = "0.00";
	}
	
	//删除
	var remove_obj = document.getElementsByClassName("remove_obj");
	var remove_all = document.querySelector(".remove_all");
	var ul = all_btn.querySelector("ul");
	for(var i = 0; i < remove_obj.length; i++){
		remove_obj[i].index = i;
		remove_obj[i].onclick = function(){
			this.parentNode.parentNode.remove();
			
			pro_info.splice(this.index,1);
			var arrs = pro_info;
			setLocalstorage('pro_info', arrs);
			sum_price(all_obj, inputs, sum, xj);
		}
	}
	remove_all.onclick = function(){
		ul.remove();
		sum_price(all_obj, inputs, sum, xj);
		for(var i = 0; i < all.length; i++){
			all[i].checked = false;
		}
		for(var i = 0; i < pro_info.length; i++){
			pro_info.splice(0,pro_info.length);
			var arrs = pro_info;
			setLocalstorage('pro_info', arrs);
		}
	}
	
	var buy = document.getElementById("buy");
	buy.onclick = function(){
		if(sum.innerHTML != "0"){
			location.href = "comfirm.html";
		}else {
			alert("请选择商品");
		}
		console.log(11)
	}
	
	
	
	
	
	
	
	
	

}
